package com.jic.point.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.MoniInterface;
import com.jic.point.model.entity.MoniInterfaceApply;
import com.jic.point.model.vo.request.MoniInterfaceRequest;
import com.jic.point.model.vo.response.MoniInterfaceResponse;
import com.jic.point.service.MoniInterfaceApplyService;
import com.jic.point.service.MoniInterfaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:33
 */
 @RestController
 @Slf4j
 @RequestMapping("/moniInterface")
 @Api(tags = " [接口监控表]控制类-PC")
public class MoniInterfaceController {
	@Autowired
	private MoniInterfaceService service;
	@Autowired
    private MoniInterfaceApplyService moniInterfaceApplyService;
	
	
     /**
      *新增记录
      * @param request 新增[接口监控表]请求参数
      * @return MoniInterfaceResponse 新增[接口监控表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[接口监控表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MoniInterfaceRequest request) throws Throwable {
     		MoniInterface moniInterface = new MoniInterface();
    		BeanUtils.copyProperties(request, moniInterface);
    	    long flag = service.save(moniInterface);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[接口监控表]请求参数
      * @return MoniInterfaceResponse 新增或修改[接口监控表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[接口监控表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody MoniInterfaceRequest request) throws Throwable {
      		MoniInterface moniInterface = new MoniInterface();
		     BeanUtils.copyProperties(request, moniInterface);
	         long flag =   service.saveOrUpdate(moniInterface);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[接口监控表]请求参数
      * @return MoniInterfaceResponse 删除[接口监控表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[接口监控表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody MoniInterfaceRequest request) throws Throwable {
      		MoniInterface moniInterface = new MoniInterface();
	     	BeanUtils.copyProperties(request, moniInterface);
	         long flag = service.deleteByPrimaryKeyLogically(moniInterface);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[接口监控表]请求参数
      * @return MoniInterfaceResponse 修改[接口监控表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[接口监控表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody MoniInterfaceRequest request) throws Throwable {
      		MoniInterface moniInterface = new MoniInterface();
	     	BeanUtils.copyProperties(request, moniInterface);
	         long flag = service.updateByPrimaryKeySelective(moniInterface);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[接口监控表]详情请求参数
      * @return MoniInterfaceResponse 查询[接口监控表]详情响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[接口监控表]详情")
      @ResponseBody
     public RestResult<MoniInterfaceResponse> getByPrimaryKey(@Validated  @RequestBody MoniInterfaceRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	MoniInterface moniInterface = new MoniInterface();
	     	BeanUtils.copyProperties(request, moniInterface);
	     	MoniInterface result = service.getByPrimaryKey(moniInterface);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     MoniInterfaceResponse bo = new MoniInterfaceResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new MoniInterfaceResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<MoniInterface>> listPage (@RequestBody MoniInterfaceRequest request) throws Throwable {
      MoniInterface param = new MoniInterface();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     @PostMapping("/listPagefreeze")
     @ApiOperation(value = " 分页查询冻结列表 ")
     @ResponseBody
     public RestResult<PageInfo<MoniInterface>> listPagefreeze (@RequestBody MoniInterfaceRequest request) throws Throwable {
      MoniInterface param = new MoniInterface();
      BeanUtils.copyProperties(request, param);
     return RestResult.success(service.listPagefreeze(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *MONI_INTERFACE 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [接口监控表] ")
      @ResponseBody
      public RestResult<List<MoniInterfaceResponse>>  listMoniInterfaceAllRecord(@RequestBody MoniInterfaceRequest request) throws Throwable {
      		MoniInterface param = new MoniInterface();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<MoniInterface> list = service.listAllRecord(param);
			List<MoniInterfaceResponse> responseList = list.stream().map(temp -> {
			MoniInterfaceResponse bo = new MoniInterfaceResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

    /**
     * 冻结申请&&解冻申请
     */
    @PostMapping("/freezeApplication")
    @ResponseBody
    public RestResult<Long> freezeApplication( @RequestBody MoniInterfaceRequest request) throws Throwable{
        //根据id查询到信息
        MoniInterface moniInterface = new MoniInterface();
        BeanUtils.copyProperties(request, moniInterface);
        MoniInterface result = service.getByPrimaryKey(moniInterface);
        //主表逻辑删除为1
        service.deleteByPrimaryKeyLogically(moniInterface);
        //赋值
        result.setSts(request.getSts());
        result.setApplyReason(request.getApplyReason());
        result.setApplyId(request.getAdminOperate().getOperator());
        //插入到申请表
        MoniInterfaceApply moniInterfaceApply = new MoniInterfaceApply();
        BeanUtils.copyProperties(result, moniInterfaceApply);
        long flag =   moniInterfaceApplyService.insertSelective(moniInterfaceApply);
        return RestResult.success(flag);
    }

     }

	
